package com.bdjk.bdmw.InfoGath.common.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bdjk.bdmw.InfoGath.common.constants.ExceptionConstants;
import com.bdjk.bdmw.InfoGath.common.dao.IJdbcDAO;
import com.bdjk.bdmw.InfoGath.common.util.DbPoolConnection;
import com.bdjk.bdmw.InfoGath.exception.BusinessException;
import com.bdjk.bdmw.InfoGath.exception.ExceptionUtil;


public class JdbcDAOImpl implements IJdbcDAO{

	/**
	 * 日志类
	 */
	//private final Logger log = LoggerFactory.getLogger(JdbcDAOImpl.class);

	protected ExceptionUtil exceptionUtil = new ExceptionUtil();

	@Override
	public int executeSQL(String sql, List<Object> params)
			throws BusinessException {

		Connection conn = null;
		PreparedStatement pstmt = null;
		int result = -1;
		ResultSet rs=null;
		try {
			conn = DbPoolConnection.getInstance().getConnection();
			pstmt = conn.prepareStatement(sql);
			this.setParams(pstmt, params);
			pstmt.execute();
			rs=pstmt.executeQuery("select @@identity as id;");
			while(rs.next()){
				result=rs.getInt("id");
				System.out.println(result);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			//log.error(e.getStackTrace().toString());
			result = -1;
			exceptionUtil.throwBusinessException(e, ExceptionConstants.ERR_CODE_1006);
		} catch (Exception e) {
			result = -1;
			e.printStackTrace();
			//throw e;
		} finally {
			DbPoolConnection.getInstance().releaseConnection(conn, pstmt, null);
		}

		return result;
	}


	/*public ResultSet executeResultSQLss(String sql, List<Object> params)
			throws BusinessException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbPoolConnection.getInstance().getConnection();
			pstmt = conn.prepareStatement(sql);
			this.setParams(pstmt, params);
			rs = pstmt.executeQuery();
			//while(rs.next()){
					//System.out.println(rs.getMetaData().getColumnName(0));
			//}
		} catch (SQLException e) {
			e.printStackTrace();
			//log.error(e.getStackTrace().toString());
			exceptionUtil.throwBusinessException(e, ExceptionConstants.ERR_CODE_1006);
		} catch (Exception e) {
			throw e;
		} finally {
			if(null != conn){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(null != pstmt){
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
		}
		return rs;
	}*/
	
	@Override
	public List<Object> executeResultSQL(String sql, List<Object> params)
			throws BusinessException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Object> result=new ArrayList<Object>();
		try {
			conn = DbPoolConnection.getInstance().getConnection();
			pstmt = conn.prepareStatement(sql);
			this.setParams(pstmt, params);
			rs = pstmt.executeQuery();
			ResultSetMetaData rsMD=rs.getMetaData();
			int cols=rsMD.getColumnCount();
			while(rs.next()){
				List<Object> rowdata=new ArrayList<Object>();
				for(int i=0;i<cols;i++){
					rowdata.add(rs.getObject(i+1));
				}
				result.add(rowdata);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			exceptionUtil.throwBusinessException(e, ExceptionConstants.ERR_CODE_1006);
		} catch (Exception be) {
			be.printStackTrace();
			//throw be;
		} finally {
			DbPoolConnection.getInstance().releaseConnection(conn, pstmt, null);
		}
		return result;
	}
	 
	/**
	 * @param pstmt
	 * @param params
	 * @throws SQLException
	 */
	protected void setParams(PreparedStatement pstmt, List<Object> params) throws SQLException {
		if (null != params && params.size() > 0) {
			for (int i = 0; i < params.size(); i++) {
				pstmt.setObject(i + 1, params.get(i));
			}
		}

	}
}
